package aishua.com.mytestapplist.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class CommonUtils {
	public static int Px2Dp(Context context, float px) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (px / scale + 0.5f);
	}
	
	public static int Dp2Px(Context context, float dp) {
	    final float scale = context.getResources().getDisplayMetrics().density; 
	    return (int) (dp * scale + 0.5f); 
	}
	
	public static String encrypt(String strSrc, String encName) {
		MessageDigest md = null;
		String strDes=null;
		try {
			byte[] bt= strSrc.getBytes("utf-8");
            if (encName==null||encName.equals("")) {
                encName="MD5";
            }
			md = MessageDigest.getInstance(encName);
			md.update(bt);
			
			strDes = bytesToHexString(md.digest());
		} catch (NoSuchAlgorithmException e) {
	        return strDes;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return strDes;
	}
	
	public static String bytesToHexString(byte[] src){
	    StringBuilder stringBuilder = new StringBuilder("");
	    if (src == null || src.length <= 0) {  
	        return null;  
	    }  
	    for (int i = 0; i < src.length; i++) {  
	        int v = src[i] & 0xFF;  
	        String hv = Integer.toHexString(v);
	        if (hv.length() < 2) {  
	            stringBuilder.append(0);  
	        }  
	        stringBuilder.append(hv);  
	    }  
	    return stringBuilder.toString();  
	}
	
}
